package org.jflame.fx.util;

import java.io.IOException;
import java.net.URL;
import java.util.Locale;
import java.util.ResourceBundle;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.paint.Color;

/**
 * javafx工具类
 * 
 * @author charles.zhang
 */
public final class FxUtils {

    public static Parent loadFXML(URL fxmlURL) {
        return loadFXML(fxmlURL, null, null, null);
    }

    /**
     * 方法注释参考{@linkplain #loadFXML(String, Node, Object, ResourceBundle)}
     * 
     * @param fxml
     * @param resourceBundle
     * @return
     */
    public static Parent loadFXML(URL fxmlURL, ResourceBundle resourceBundle) {
        return loadFXML(fxmlURL, null, null, resourceBundle);
    }

    /**
     * 方法注释参考{@linkplain #loadFXML(String, Node, Object, ResourceBundle)}
     * 
     * @param fxml
     * @param controller
     * @return
     */
    public static Parent loadFXML(URL fxml, Object controller) {
        return loadFXML(fxml, null, controller, null);
    }

    public static Parent loadFXML(URL fxml, Object controller, ResourceBundle resourceBundle) {
        return loadFXML(fxml, null, controller, resourceBundle);
    }

    /**
     * 加载一个fxml文件
     * 
     * @param fxml fxml fxml文件相对类路径
     * @param root 根节点控件
     * @param controller 关联ui controller
     * @param resourceBundle 指定多语言资源文件,不指定时使用全局资源文件
     * @return 返回生成的Parent
     */
    public static Parent loadFXML(String fxml, Node root, Object controller, ResourceBundle resourceBundle) {
        String fxmlPath = fxml.endsWith(".fxml") ? fxml : fxml + ".fxml";
        if (fxmlPath.charAt(0) == '/') {
            fxmlPath = fxmlPath.substring(1);
        }
        ClassLoader classLoader = null;
        try {
            classLoader = Thread.currentThread()
                    .getContextClassLoader();
        } catch (Exception e) {
        }
        if (classLoader == null) {
            if (controller != null) {
                classLoader = controller.getClass()
                        .getClassLoader();
            } else {
                classLoader = FxUtils.class.getClassLoader();
            }
        }

        URL fxmlUrl = classLoader.getResource(fxmlPath);

        /*System.out.println("===m" + ModuleLayer.boot()
                .findModule("org.jflame.devAide")
                .get()
                .getClassLoader()
                .getResource("org/jflame/devAide/plugin/cron/cronTool.fxml"));*/
        return loadFXML(fxmlUrl, root, controller, resourceBundle);
    }

    public static Parent loadFXML(URL fxmlUrl, Node root, Object controller, ResourceBundle resourceBundle) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(fxmlUrl);
            if (resourceBundle != null) {
                fxmlLoader.setResources(resourceBundle);
            }
            if (root != null) {
                fxmlLoader.setRoot(root);
            }
            if (controller != null) {
                fxmlLoader.setController(controller);
            }
            return fxmlLoader.load();
        } catch (IllegalStateException | IOException e) {
            throw new RuntimeException("加载fxml文件异常：" + fxmlUrl, e);
        }
    }

    public static FXMLLoader createFXMLLoader(URL fxmlUrl, ResourceBundle resourceBundle) {

        FXMLLoader fxmlLoader = new FXMLLoader(fxmlUrl);
        if (resourceBundle != null) {
            fxmlLoader.setResources(resourceBundle);
        }

        return fxmlLoader;

    }

    /**
     * 将Color转为ARGB模式的16进制值
     * 
     * @param color Color
     * @return
     */
    public static int colorToArgb(Color color) {
        int r = (int) Math.round(color.getRed() * 255.0);
        int g = (int) Math.round(color.getGreen() * 255.0);
        int b = (int) Math.round(color.getBlue() * 255.0);
        int o = (int) Math.round(color.getOpacity() * 255.0);
        String hexColor = String.format("%02x%02x%02x%02x", o, r, g, b);
        return Integer.parseUnsignedInt(hexColor, 16);
    }

    /**
     * 将Color转为RGB rgba(%d, %d, %d, %f)
     * 
     * @param color
     * @return
     */
    public static String colorToRGBA(Color color) {
        return String.format((Locale) null, "rgba(%d, %d, %d, %f)", (int) Math.round(color.getRed() * 255),
                (int) Math.round(color.getGreen() * 255), (int) Math.round(color.getBlue() * 255), color.getOpacity());
    }

    /**
     * 隐藏控件
     * 
     * @param node
     */
    public static void hide(Node node) {
        node.setVisible(false);
        node.setManaged(false);
    }

    /**
     * 显示控件
     * 
     * @param node
     */
    public static void show(Node node) {
        node.setVisible(true);
        node.setManaged(true);
    }

}
